package com.example.eurekaclient.controller;


import cn.ucloud.common.pojo.Account;
import cn.ucloud.common.pojo.Param;
import com.example.eurekaclient.util.SignatureExt;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.MultipartBodyBuilder;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

@RestController

public class UTTErrorController {

    private Account account;

    @ResponseBody
    @RequestMapping(value="/tterror",method= RequestMethod.POST,produces="application/json;charset=UTF-8")
    public String getImageByJSON(@RequestParam(value = "url",required = true) String Url) throws JSONException {

        RestTemplate restTemplate = new RestTemplate();

        long timestamp = new Date().toInstant().getEpochSecond(); //unix timestamp



        //Account account = new Account("BZUNQWSDjKo6aOE7zA6tc0IhnE46BkP8Mu7nVnG5","BmwQd7dRrVJHT0c83hykfI7GyOYlhtGoI5aizTGezruJEkZOz3kXYmGCH6MPITESrU");
        Account account = new Account("BmwQd7dRrVJHT0c83hykfI7GyOYlhtGoI5aizTGezruJEkZOz3kXYmGCH6MPITESrU","BZUNQWSDjKo6aOE7zA6tc0IhnE46BkP8Mu7nVnG5");
       // String url = "http://api.uai.ucloud.cn/tupu/censor";
        Param[] params = {
                new Param("Url",Url),
                new Param("ResourceId","uaialg-jh3kborg"),
                new Param("PublicKey","BZUNQWSDjKo6aOE7zA6tc0IhnE46BkP8Mu7nVnG5"),
                new Param("Timestamp",timestamp)
        };

        String paramAfterSignature = SignatureExt.getSign(params,account);
        System.out.println(paramAfterSignature);

        String ucloudurl = "http://api.uai.ucloud.cn/tupu/censor";
        //String ResourceId = "uaialg-qssauopg";
        //String PublicKey =  "BZUNQWSDjKo6aOE7zA6tc0IhnE46BkP8Mu7nVnG5";
        //long Timestamp = System.currentTimeMillis();

        String Scenes = "tupu_terror";
        String Method ="url";


       // String Url = "https://static.ucloud.cn/d94fcd382ef6475782d9a67972d26168.png";
        JSONObject dataobj = new JSONObject();
        dataobj.put("Scenes",Scenes);
        dataobj.put("Method",Method);
        dataobj.put("Url",Url);

        HttpHeaders headers = new HttpHeaders();
//        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
//        MediaType type = MediaType.parseMediaType("Multipart/Form-data");
//        headers.setContentType(type);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        headers.add("Signature",paramAfterSignature);
        headers.add("PublicKey","BZUNQWSDjKo6aOE7zA6tc0IhnE46BkP8Mu7nVnG5");
        headers.add("ResourceId","uaialg-jh3kborg");
        headers.add("Timestamp",timestamp+"");


        MultipartBodyBuilder multipartBodyBuilder = new MultipartBodyBuilder();
        // ----------------- 表单 part
        multipartBodyBuilder.part("name", "KevinBlandy");
        /**
         * 每个表单项都有独立的header
         * 在这个表单项后额外添加了一个header
         */
        multipartBodyBuilder.part("ProductNames", Scenes);

        // ----------------- 文件 part
        // 从磁盘读取文件
        multipartBodyBuilder.part("Method", Method);
        // 从classpath读取文件
        multipartBodyBuilder.part("Url", Url);


        // build完整的消息体
        MultiValueMap<String, HttpEntity<?>> multipartBody = multipartBodyBuilder.build();
        HttpEntity<MultiValueMap<String,  HttpEntity<?>>> request = new HttpEntity<MultiValueMap<String, HttpEntity<?>>>(multipartBody, headers);

//        ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://localhost/test", multipartBody, String.class);
//
//        System.out.println(responseEntity);



//        HttpEntity<String> request = new HttpEntity<String>(dataobj.toString(), headers);
//        HttpEntity<String> request = new HttpEntity<String>(multipartBody, headers);

//        System.out.println(restTemplate.postForObject(ucloudurl, request,JSONObject.class));
//        System.out.println(restTemplate.postForObject(ucloudurl, request,String.class));

        return restTemplate.postForObject(ucloudurl, request,String.class);
    }


}
